package com.zte.util;

import java.text.SimpleDateFormat;
import java.util.Date;

public class TimeComparator {

    /**
     * 返回值为1为predate小于aftrdate，返回值为-1为两个时间相等，返回值为0说明predate大于aftrdate
     * @param predate
     * @param aftrdate
     * @return
     */
    public static int comparatorTime(Date predate,Date aftrdate){
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
        int flag=0;
        try {
            Date begin = df.parse(df.format(predate));
            Date end = df.parse(df.format(aftrdate));
            if (begin.getTime()>end.getTime()){
                flag = 1;
            }else if (begin.getTime() == end.getTime()){
                flag = 0;
            }else {
                flag = -1;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return flag;
    }

    public static Date[] changeTime(Date predate,Date aftrdate){
        Date[] dates = new Date[2];
        dates[0] = predate;
        dates[1] = aftrdate;
        try {
            if (predate != null && aftrdate != null) {
                int flag = comparatorTime(predate, aftrdate);
                if (flag == 1) {
                    dates[0] = aftrdate;
                    dates[1] = predate;
                } else if (flag == -1) {
                    dates[0] = predate;
                    dates[1] = aftrdate;
                } else {
                    dates[0] = dates[1] = predate;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dates;
    }

}
